<template>
  <div class="login-container">
    <div class="login_box">
      <div class="login_header">
        <img src="../assets/logo.png">
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login_form" label-width="0">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <div class="buts">
            <el-button type="primary" @click="log">登录</el-button>
            <el-button type="info" @click="resetloginForm">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      loginForm:{
        username:'admin',
        password:'123456'
      },
      loginFormRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  methods:{
    resetloginForm(){
      this.$refs.loginFormRef.resetFields();
    },
    log(){
      this.$refs.loginFormRef.validate(async vaild =>{
        if (!vaild) return;
        const {data:res}= await this.$http.post('login',this.loginForm);
        if (res.meta.status !== 200) return this.$message.error("登陆失败！");
        this.$message.success("登陆成功！");
        window.sessionStorage.setItem("token",res.data.token);
        this.$router.push("/home");
      });
    }
  }
};
</script>

<style lang="css" scoped>
.login-container{
  background-image: url("../assets/002.png");
  background-repeat: no-repeat;
  background-size: 100%;
  width: 100%;
  height:100%;
  margin: 0;
  padding: 0;
  position: relative;
}
.login_box{
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
.login_header{
  width: 130px;
  height: 130px;
  border-radius: 50%;
  position: absolute;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: white;
  border: 1px solid #eee;
}
.login_header img{
  width: 100%;
  height: 100%;
  border-radius: 50%;
}
.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.buts{
  width: 150px;
  margin: auto;
}
</style>
